//
//  SEUserInfoViewModel.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/4.
//

import HandyJSON

class SEUserInfoViewModel: SEBaseViewModel {

    var type: SEUserInfoViewControllerType = .register

    var model = SEUserInfoModel()

    var loginModel = SELoginModel()

    var authViewModel = SEAuthViewModel()

    func saveAuthToken() {
        UserDefaults.authToken = loginModel.accessToken
    }
    func saveUserId() {
        let isFemale = model.idCard.subString(index: model.idCard.count - 2, length: 1).intValue % 2 == 0
        UserDefaults.userFemale = isFemale
        UserDefaults.userId = model.userId
    }
    func removeAuthToken() {
        UserDefaults.removeAuthToken()
    }
    
    var avatarPlaceholder: UIImage? {
        UserDefaults.userFemale ? Asset.iconDoctorAvatarFemale.image : Asset.iconDoctorAvatarMale.image
    }

    var loginHandler: ((SEHttpStatus) -> Void)?

    var editHandler: ((SEHttpStatus) -> Void)?

    var administrative: String {
        return "\(model.provinceName)\(model.cityName)\(model.regionName)"
    }

    var dataSource: [[SEDoctorTitleTextFieldCellModel]] {
        return [
            [
                SEDoctorTitleTextFieldCellModel(title: "姓名",
                                                 placeHolder: "请输入姓名",
                                                 cellType: .normal,
                                                 text: model.name,
                                                 editEnable: type == .register),
                SEDoctorTitleTextFieldCellModel(title: "身份证号",
                                                 placeHolder: "请输入身份证号",
                                                 cellType: .normal,
                                                 text: model.idCard,
                                                 editEnable: type == .register),
                SEDoctorTitleTextFieldCellModel(title: "行政区域",
                                                 placeHolder: "请选择行政区域",
                                                 cellType: .tap,
                                                 text: administrative),
                SEDoctorTitleTextFieldCellModel(title: "执业医院",
                                                 placeHolder: "请选择您所在执业医院",
                                                 cellType: .tap,
                                                 text: model.hospitalName),
            ],
            [
                SEDoctorTitleTextFieldCellModel(title: "院内科室",
                                                 placeHolder: "请输入院内科室",
                                                 cellType: .normal,
                                                 text: model.deptName),
                SEDoctorTitleTextFieldCellModel(title: "标准科室",
                                                 placeHolder: "请选择标准科室",
                                                 cellType: .tap,
                                                 text: model.standardName),
                SEDoctorTitleTextFieldCellModel(title: "我的职称",
                                                 placeHolder: "请输入",
                                                 cellType: .tap,
                                                 text: model.jobTitle),
            ],
            [
                SEDoctorTitleTextFieldCellModel(title: "我的介绍",
                                                 cellType: .title),
            ],
            [
                SEDoctorTitleTextFieldCellModel(title: "擅长",
                                                 cellType: .title),
            ]
        ]
    }

    var checkNext: String? {
        if model.name.isEmpty {
            return "请输入姓名"
        }
        if model.idCard.isEmpty {
            return "请输入身份证号"
        }
        if model.hospitalId.isEmpty {
            return "请选择执业医院"
        }
        if model.deptName.isEmpty {
            return "请输入院内科室"
        }
        if model.standardId.isEmpty {
            return "请选择科室"
        }
        if model.jobTitle.isEmpty {
            return "请输入职称"
        }
        return nil
    }

    /// 注册
    func requestRegister() {
        if let msg = checkNext {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.request(SEAuthAPI.register(authViewModel: authViewModel, userInfoModel: model), model: [String: String].self, withLoading: true) { (result, status) in
            switch status {
            case .success:
                let dic = result ?? ["": ""]
                self.model.id = dic["id"] ?? ""
                self.requestLogin()
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    /// 登录
    func requestLogin() {
        SEHttpManager.shared.request(SEAuthAPI.login(account: authViewModel.account, code: authViewModel.code), model: SELoginModel.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.loginModel = model ?? SELoginModel()
                self.saveAuthToken()
            default:
                break
            }
            self.loginHandler?(status)
        }
    }

    /// 查询用户信息
    func requestUserInfo() {
        SEHttpManager.shared.request(SEAuthAPI.userInfo, model: [SEUserInfoModel].self, withLoading: withLoading) { models, status in
            switch status {
            case .success:
                guard let models = models, models.count == 1 else { return }
                self.model = models[0]
                self.saveUserId()
            default:
                if self.type == .register {
                    self.removeAuthToken()
                }
            }
            self.updateData?(status)
        }
    }

    /// 编辑用户信息
    func editUserInfo() {
        SEHttpManager.shared.request(SEAuthAPI.editUserInfo(userInfoModel: model), model: Any.self, withLoading: true) { result, status in
            self.editHandler?(status)
        }
    }
}


